<?php
/**
 * Created by PhpStorm.
 * User: man0sions
 * Date: 16/8/25
 * Time: 下午7:52
 */

namespace src\base;


use src\base\api\Registry;
use src\base\db\Proxy;

/**
 * 简单工厂,构造全局单例对象
 * Class Factory
 * @package src\base
 */
class Factory
{
    private static $db;

    /**
     * 配合代理Proxy 实现 sql 自动读写分离
     * @param string $type
     * @return null
     */
    static function getDb($type = 'proxy')
    {

        $main_conf = Registry::get('main_config');
        if ($type == 'proxy') {
            $key = "\\src\\base\\db\\Proxy";
            $db = Registry::get($key);

            if (!$db) {
                $db = new $key();
                Registry::set($key, $db);
            }
            return $db;
        }

        elseif($type=='slave')
        {
            $rand = array_rand($main_conf['db']['slave']);
            $db_conf = $main_conf['db']['slave'][$rand];

        }
        elseif($type=='master')
            $db_conf = $main_conf['db']['master'];
        $key =   "\\src\\base\\db\\Pdo";

        self::$db = new $key();
        self::$db->connect($db_conf['host'],$db_conf['user'],$db_conf['passwd'],$db_conf['dbname']);
      return self::$db;
    }

    static function getRequest()
    {

    }

    static function getCommand()
    {

    }
}